/*
 * ResourceGuildBuff.cpp
 *
 *  Created on: 2014. 7. 30.
 *      Author: mega
 */

#include <algorithm>
#include <Database/Static.h>

#include "ResourceGuildBuff.h"

namespace GuildServer { namespace NResource {

ResourceGuildBuff::ResourceGuildBuff()
{
}

ResourceGuildBuff::~ResourceGuildBuff()
{
	std::for_each(clsFlyMap.begin(), clsFlyMap.end(), [&](BUFFMAP::value_type & pair) {
		delete pair.second;
	});
}

void
ResourceGuildBuff::Clear()
{
	std::for_each(clsFlyMap.begin(), clsFlyMap.end(), [&](BUFFMAP::value_type & pair) {
		delete pair.second;
	});
	clsFlyMap.clear();
}

bool
ResourceGuildBuff::LoadResource()
{
	Database::ResultSet res = Database::Execute(static_cast<int>(DatabaseType::GUILD_MASTER), "SELECT * FROM resource_guild_buff");

	if (0 != res.GetSQLError())
		return false;

	if (0 == res.GetRowCount())
		return false;

	this->Clear();

	for (Database::ResultSet::iterator iter = res.begin(); iter != res.end(); iter++)
	{
		Flyweight * pFly = new Flyweight;
		std::string nationalCode;

		nationalCode				= iter->getString("NATIONAL_CODE");

		pFly->guild_grade			= iter->getInt("guild_grade");

		pFly->skill_type0_tid		= iter->getInt("skill_type0_tid");
		pFly->skill_type1_tid		= iter->getInt("skill_type1_tid");
		pFly->skill_type2_tid		= iter->getInt("skill_type2_tid");
		pFly->skill_type3_tid		= iter->getInt("skill_type3_tid");
		pFly->skill_type4_tid		= iter->getInt("skill_type4_tid");
		pFly->skill_type5_tid		= iter->getInt("skill_type5_tid");
		pFly->skill_type6_tid		= iter->getInt("skill_type6_tid");
		pFly->skill_type7_tid		= iter->getInt("skill_type7_tid");

		pFly->skill_type0_rate		= iter->getInt("skill_type0_rate");
		pFly->skill_type1_rate		= iter->getInt("skill_type1_rate");
		pFly->skill_type2_rate		= iter->getInt("skill_type2_rate");
		pFly->skill_type3_rate		= iter->getInt("skill_type3_rate");
		pFly->skill_type4_rate		= iter->getInt("skill_type4_rate");
		pFly->skill_type5_rate		= iter->getInt("skill_type5_rate");
		pFly->skill_type6_rate		= iter->getInt("skill_type6_rate");
		pFly->skill_type7_rate		= iter->getInt("skill_type7_rate");

		pFly->skill_level1_rate		= iter->getInt("skill_level1_rate");
		pFly->skill_level2_rate		= iter->getInt("skill_level2_rate");
		pFly->skill_level3_rate		= iter->getInt("skill_level3_rate");
		pFly->skill_level4_rate		= iter->getInt("skill_level4_rate");
		pFly->skill_level5_rate		= iter->getInt("skill_level5_rate");
		pFly->skill_level6_rate		= iter->getInt("skill_level6_rate");
		pFly->skill_level7_rate		= iter->getInt("skill_level7_rate");
		pFly->skill_level8_rate		= iter->getInt("skill_level8_rate");
		pFly->skill_level9_rate		= iter->getInt("skill_level9_rate");
		pFly->skill_level10_rate	= iter->getInt("skill_level10_rate");

		clsFlyMap[pFly->guild_grade] = pFly;
	}

	return true;
}

ResourceGuildBuff::Flyweight *
ResourceGuildBuff::GetFly(int guild_grade)
{
	auto iter = clsFlyMap.find(guild_grade);
	if (iter == clsFlyMap.end())
		return NULL;

	return iter->second;
}

int
ResourceGuildBuff::GetBuffType(Flyweight * pFly)
{
	int nRand = rand() % 1000;
	int accRate = 0;
	int idx = 0;

	for (; idx < 8; idx++)
	{
		switch (idx)
		{
		case 0 : accRate += pFly->skill_type0_rate; break;
		case 1 : accRate += pFly->skill_type1_rate; break;
		case 2 : accRate += pFly->skill_type2_rate; break;
		case 3 : accRate += pFly->skill_type3_rate; break;
		case 4 : accRate += pFly->skill_type4_rate; break;
		case 5 : accRate += pFly->skill_type5_rate; break;
		case 6 : accRate += pFly->skill_type6_rate; break;
		case 7 : accRate += pFly->skill_type7_rate; break;
		}

		if (nRand < accRate)
			break;
	}

	int buffType = 0;

	switch (idx)
	{
	case 0 : buffType = pFly->skill_type0_tid; break;
	case 1 : buffType = pFly->skill_type1_tid; break;
	case 2 : buffType = pFly->skill_type2_tid; break;
	case 3 : buffType = pFly->skill_type3_tid; break;
	case 4 : buffType = pFly->skill_type4_tid; break;
	case 5 : buffType = pFly->skill_type5_tid; break;
	case 6 : buffType = pFly->skill_type6_tid; break;
	case 7 : buffType = pFly->skill_type7_tid; break;
	}

	return buffType;
}

int
ResourceGuildBuff::GetBuffLevel(Flyweight * pFly)
{
	int nRand = rand() % 1000;
	int accRate = 0;
	int buffLevel = 1;

	for (; buffLevel <= 10; buffLevel++)
	{
		switch (buffLevel)
		{
		case 1 : accRate += pFly->skill_level1_rate; break;
		case 2 : accRate += pFly->skill_level2_rate; break;
		case 3 : accRate += pFly->skill_level3_rate; break;
		case 4 : accRate += pFly->skill_level4_rate; break;
		case 5 : accRate += pFly->skill_level5_rate; break;
		case 6 : accRate += pFly->skill_level6_rate; break;
		case 7 : accRate += pFly->skill_level7_rate; break;
		case 8 : accRate += pFly->skill_level8_rate; break;
		case 9 : accRate += pFly->skill_level9_rate; break;
		case 10 : accRate += pFly->skill_level10_rate; break;
		}

		if (nRand < accRate)
			break;
	}

	return buffLevel;
}

} /* namespace NResource */ } /* namespace GuildServer */
